package com.fosss.queue;

import java.util.Scanner;

/**
 *   目前数组使用一次就不能用，需要改造成环形队列
 */
public class ArrayQueueDemo {
    public static void main(String[] args) {
        //构造队列
        ArrayQueue queue = new ArrayQueue(3);
        char input=' ';
        boolean flag=true;
        while (flag) {
            System.out.println("a(add):添加数值");
            System.out.println("g(get):获取数值");
            System.out.println("h(head):获取队列头");
            System.out.println("s(show):遍历队列");
            System.out.println("e(exit):退出程序");
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入选项：");
            input=scanner.next().charAt(0);

            switch (input) {
                case 'a':
                    try {
                        System.out.println("请输入要添加的值：");
                        int i = scanner.nextInt();
                        queue.addQueue(i);
                    }catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'g':
                    try{
                        System.out.println(queue.getQueue());
                    }catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try {
                        System.out.println(queue.getHead());
                    }catch (Exception e){
                        System.out.println(e.getMessage());
                    }
                    break;
                case 's':
                    try {
                        queue.show();
                    }catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    scanner.close();
                    flag=false;
                    break;
                default:
                    System.out.println("选择错误");

            }
        }

        System.out.println("再见了我的老baby~e");

    }
}

class ArrayQueue{
    private int maxCapacity;//最大容量
    private int front;//队列头
    private int rear;//队列尾
    private int[] queue;//队列

    //队列的构造器
    public ArrayQueue(int maxCapacity){
        this.maxCapacity = maxCapacity;
        front=-1;//指向第一个元素的前一个位置
        rear = -1;//指向第一个元素的前一个位置
        queue=new int[maxCapacity];
    }
    //判断队列是否满
    public boolean isFull(){
        return rear==maxCapacity-1;
    }
    //判断队列是否为空
    public boolean isEmpty(){
        return front==rear;
    }
    //添加值
    public void addQueue(int n){
        //判断是否满
        if(isFull()){
            throw new RuntimeException("队列已满，不可添加");
        }
        rear++;
        queue[rear]=n;
    }
    //取值
    public int getQueue(){
        //判断是否空
        if(isEmpty()){
            throw new RuntimeException("队列为空，无法取值");
        }
        front++;
        return queue[front];
    }
    //展示队列头
    public int getHead(){
        if(isEmpty()){
            throw new RuntimeException("队列为空，无法取值");
        }
        return queue[front+1];
    }
    //遍历队列
    public void show(){
        if(isEmpty()){
            throw new RuntimeException("队列为空，无法遍历");
        }
        for (int i = 0; i < queue.length; i++) {
            System.out.printf("queue[%d]=%d",i,queue[i]);
            System.out.println();
        }
    }

}



















